// src/main/java/tech/aistar/service/AfterSaleServiceImpl.java
package tech.aistar.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.mapper.AfterSaleMapper;
import tech.aistar.pojo.AfterSale;
import java.util.List;

@Service
public class AfterSaleServiceImpl implements AfterSaleService {

    @Autowired
    private AfterSaleMapper afterSaleMapper;

    @Override
    public List<AfterSale> getAfterSaleList(String orderId, String buyerName, Integer type, Integer status, Integer page, Integer size) {
        Integer offset = (page - 1) * size;
        return afterSaleMapper.selectAfterSaleList(orderId, buyerName, type, status, offset, size);
    }

    @Override
    public Long getAfterSaleCount(String orderId, String buyerName, Integer type, Integer status) {
        return afterSaleMapper.countAfterSales(orderId, buyerName, type, status);
    }

    @Override
    public AfterSale getAfterSaleById(Integer afterSaleId) {
        return afterSaleMapper.getById(afterSaleId);
    }

    @Override
    @Transactional
    public boolean updateAfterSaleStatus(Integer afterSaleId, Integer status) {
        try {
            if (status == 5) {
                // 状态为5时删除记录
                return afterSaleMapper.deleteById(afterSaleId) > 0;
            } else {
                // 其他状态更新状态
                return afterSaleMapper.updateStatus(afterSaleId, status) > 0;
            }
        } catch (Exception e) {
            throw new RuntimeException("更新售后状态失败: " + e.getMessage());
        }
    }
}
